rm(list = ls())
library(ggplot2)

lik_fun <- function(phi, y){
  Tn <- length(y)
  likehood <- -(Tn/2)*log(2*pi) + 0.5*log(1-phi^2) - 0.5*(1-phi^2)*y[1]^2 -
    0.5*sum((y - phi*dplyr::lag(y))^2, na.rm = T)
  return(likehood)
}

y <- c(0.8,0.2,-1.2,-0.4,0)
# 似然值
lik_val <- sapply(seq(-0.9,0.9,0.1), lik_fun, y = y)

ggplot(data.frame(x = seq(-0.9,0.9,0.1), y = lik_val), aes(x =x, y = y)) +
  geom_point() + geom_hline(yintercept = -5, color = I('blue')) +
  labs(x = 'phi', y = 'L') +
  theme_bw()
ggsave('../EconomicGrowthLecture/lik_val.png')
